package com.fqgj.xjd.user.common.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * name:日期
 * auth:ting.li@qianli-inc.com
 * date:2018/1/5 17:25
 * version: v1.0.0
 **/
public class DateUtil {

    /**
     * 获取当前时间距离当天24点的差值（到秒）
     * @param date
     * @return
     */
    public static Integer getDiffDayTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        Integer diffTime = Long.valueOf((calendar.getTime().getTime() - date.getTime())/1000).intValue();
        return diffTime;
    }

    /**
     * 求日期差值
     * @param d1
     * @param d2
     * @return
     */
    public static Boolean afterDiffDay(Date d1,Date d2,Integer day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);
        calendar.add(Calendar.DATE,-day);
        return calendar.getTime().after(d2);
    }

    /**
     * 判断两个日期之间间隔时间
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        int days = 0;
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            days = timeDistance + (day2 - day1);
        } else {
            days = day2 - day1;
        }

        return days;
    }

    public static void main(String[] args) {
        String a = "2019-02-04";
        int b = differentDays(new Date(), com.fqgj.common.utils.DateUtil.getDate(a, "yyyy-MM-dd"));
        System.out.println(b);
    }

}
